/**
 * Created by chenzui on 2017/3/28.
 */
var $title = $('#adTitle');
var $adUrl = $('#adUrl');
var $adLocation = $('#adLocation');
var $adEndTime = $('#adEndTime');
var $uploadFile = $('#uploadFile');
var $submitAdBtn = $('#submitAdBtn');
var imgUrl = "";
function ajaxFileUpload(fileInput){
    var tmp = $(fileInput);
    var id = tmp.attr("id");
    $.ajaxFileUpload({
        url:'/file/upload',            //需要链接到服务器地址
        fileElementId:$(fileInput).attr("id"),
        secureuri: false,
        dataType: 'json',                                                //服务器返回的格式，可以是json
        date:$('#updateForm').serialize(),
        // async:true,
        success: function (data,status){                                 //相当于java中try语句块的用法
            imgUrl = data.data;
            alert("success");
        },
        error: function (data, status, e){                               //相当于java中catch语句块的用法
            alert("fail");

        }
    });
    $('#'+id+'').before(tmp);
    $('#'+id+'').remove();
}

function add() {
    var title = $title.val().trim();
    var url = $adUrl.val().trim();
    var type = $adLocation.val().trim();
    var endTime = $adEndTime.val();
    var ifLink = 1;
    if (imgUrl == ""){
        alert("请选择广告图片");
        return;
    }

    if(url != ""){

    }else {
        ifLink = 0;
    }

    if(title == ""){
        alert("请输入标题");
        return;
    }

    var param = {
        title:title,
        imgUrl:imgUrl,
        url:url,
        type:type,
        endTime:endTime,
        ifLink:ifLink,
        content:editor.$txt.html()
    }

    Ajax.ajax({
        url: "/ad/add",
        data: param,
        type: "post",
        dataType: 'json',
        success:function(res){
            console.log(res);
            if (res.resultCode == 0) {

            }else {

            }
        }

    });
}

$(function () {
    $adEndTime.datepicker({
        format: 'yyyy-mm-dd'
    });

    $uploadFile.on('change', function() {
        ajaxFileUpload(this);
    });

    $submitAdBtn.on('click',function () {
        add();
    });

})